//
//  ViewController.m
//  SoundPlayer
//
//  Created by Justsoso on 16/4/5.
//  Copyright © 2016年 Justsoso. All rights reserved.
//

#import "ViewController.h"

#import <AudioUnit/AudioUnit.h>

#import <AVFoundation/AVFoundation.h>

@interface ViewController ()
{
    SystemSoundID soundID;
}
@property (weak, nonatomic) IBOutlet UILabel *statusLabel;
@property (weak, nonatomic) IBOutlet UILabel *infoLabel;
@property (weak, nonatomic) IBOutlet UISlider *slider;

@property (nonatomic,strong) AVAudioPlayer *player;

@property (nonatomic,strong) AVAudioRecorder *recorder2;
@property (nonatomic,strong) AVAudioPlayer *player2;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}


#pragma mark - 播放短声音
- (IBAction)playSystemSound:(id)sender
{
    if (soundID == 0)
    {
        NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"AlertChordStroke" withExtension:@"wav"];
        AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)(fileURL), &soundID);
    }
    
    AudioServicesPlaySystemSound(soundID);
}
- (IBAction)vibrate:(id)sender
{

   AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}


#pragma mark - 播放长声音
- (IBAction)play:(id)sender
{
    NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"charleston1925_64kb" withExtension:@"mp3"];
    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
    
    self.slider.minimumValue = 0;
    self.slider.maximumValue = self.player.duration;
    [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(updateInfo) userInfo:nil repeats:YES];
    [self.player play];
    
}
- (IBAction)stop:(id)sender
{
    [self.player stop];
}

- (void)updateInfo
{
    self.slider.value = self.player.currentTime;
    self.infoLabel.text = [NSString stringWithFormat:@"%.1f/%.1f",self.player.currentTime,self.player.duration];
}
- (IBAction)sliderChanged:(id)sender
{
    self.player.currentTime = self.slider.value;
}

#pragma mark - 录制相关

- (NSURL *)fileURL
{
    NSString *docPath = [NSHomeDirectory() stringByAppendingString:@"/Documents"];
    NSString *filePath = [docPath stringByAppendingString:@"/1.caf"];
    NSURL *fileURL = [NSURL URLWithString:filePath];
    
    return fileURL;
}

- (IBAction)recordBtnPressed:(id)sender
{
    if (self.recorder2.recording)
    {
        return;
    }
    if (self.player2.playing)
    {
        return;
    }
    
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:nil];
    [[AVAudioSession sharedInstance] setActive:YES error:nil];
    
    //设置一下录制的设置
    NSMutableDictionary *settings = [NSMutableDictionary dictionary];
    [settings setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM]
                forKey:AVFormatIDKey];
    [settings setValue:[NSNumber numberWithFloat:44100.0]
                forKey:AVSampleRateKey]; //采样率
    [settings setValue:[NSNumber numberWithInt:1]
                forKey:AVNumberOfChannelsKey];//通道的数目
    [settings setValue:[NSNumber numberWithInt:16]
                forKey:AVLinearPCMBitDepthKey];//采样位数  默认 16
    [settings setValue:[NSNumber numberWithBool:NO]
                forKey:AVLinearPCMIsBigEndianKey];//大端还是小端 是内存的组织方式
    [settings setValue:[NSNumber numberWithBool:NO]
                forKey:AVLinearPCMIsFloatKey];//采样信号是整数还是浮点数
    
    self.recorder2 = [[AVAudioRecorder alloc] initWithURL:[self fileURL] settings:settings error:nil];
    
    [self.recorder2 record];
    
    self.statusLabel.text = @"录音中";
}
- (IBAction)stopBtnPressed:(id)sender
{
    if (self.recorder2.recording)
    {
        [self.recorder2 stop];
    }
    
    if (self.player2.playing)
    {
        [self.player2 stop];
    }
    
    self.statusLabel.text = @"停止了";
}
- (IBAction)playBtnPressed:(id)sender
{
    if (self.recorder2.recording)
    {
        return;
    }
    if (self.player2.playing)
    {
        return;
    }
    
    self.player2 = [[AVAudioPlayer alloc] initWithContentsOfURL:[self fileURL] error:nil];
    
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    [[AVAudioSession sharedInstance] setActive:YES error:nil];
    
    [self.player2 play];
    
    self.statusLabel.text = @"播放中";
}

@end
